package me.mouse.gokistats.talent;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;

import me.mouse.gokistats.config.Config;
import me.mouse.gokistats.event.RegisterTalentEvent;

public class RegisterTalent {
	private static List<Talent> registerTalent = new ArrayList<Talent>();

	public static void registerTalent(Talent[] t) {
		Bukkit.getLogger().info("[GokiStats] 天赋注册中……");
		Talent[] arrayOfTalent = t;
		int j = t.length;
		for (int i = 0; i < j; i++) {
			Talent talent = arrayOfTalent[i];
			RegisterTalentEvent e = new RegisterTalentEvent(talent);
			Bukkit.getServer().getPluginManager().callEvent(e);
			boolean b = true;
			for (Talent registerT : registerTalent) {
				if (talent.getName() == registerT.getName()) {
					Bukkit.getLogger().info(
							"[GokiStats]注册天赋 " + talent.getName() + " 失败,错误原因：名称与 " + registerT.getName() + " 相同.");
					b = false;
				}
				if (talent.getID() == registerT.getID()) {
					Bukkit.getLogger().info(
							"[GokiStats]注册天赋 " + talent.getName() + " 失败,错误原因：ID与 " + registerT.getName() + " 相同.");
					b = false;
				}
				if (talent.getSlot() == registerT.getSlot()) {
					Bukkit.getLogger().info(
							"[GokiStats]注册天赋 " + talent.getName() + " 失败,错误原因：菜单位置与 " + registerT.getName() + " 相同.");
					b = false;
				}
			}
			if (e.isCancelled()) {
				b = false;
			}
			if (!talent.getEnable()) {
				b = false;
			}
			if (talent.getFormula() == null) {
				talent.setFormula(Config.getFormula());
			}
			if (talent.getPayment() == null) {
				talent.setPayment(Config.getPaymentType());
			}
			if (talent.getDisplayName() == null) {
				talent.setDisplayName(talent.getName());
			}
			if (b) {
				registerTalent.add(talent);
				Bukkit.getLogger().info("[GokiStats] 已注册天赋 " + talent.getDisplayName());
			}
		}
	}

	public static Talent getTalent(String Name) {
		for (Talent t : registerTalent) {
			if (Name.equalsIgnoreCase(t.getName())) {
				return t;
			}
		}
		return null;
	}

	public static Talent getTalent(int ID) {
		for (Talent t : registerTalent) {
			if (ID == t.getID()) {
				return t;
			}
		}
		return null;
	}

	public static List<Talent> getRegisterTalent() {
		return registerTalent;
	}

	public static void clearRegisterTalent() {
		registerTalent.clear();
	}
}
